<?php
namespace app\admin\controller;
use think\Db;
/**
 * 商家
 * Class Member
 */
class ShopController extends BaseController {
    //商家列表
	public function agents($agent_name='',$mobile='',$status=''){
		$where=[];
		if($agent_name){
			$where['agent_name']=array('like','%'.$agent_name.'%');
		}
		$this->assign('agent_name',$agent_name);
		if($mobile){
			$where['mobile']=array('like','%'.$mobile.'%');
		}
		$this->assign('mobile',$mobile);
		if($status){
			$where['status']=$status;
		}
		$this->assign('status',$status);
		
		$list=db("agent")->where($where)->paginate(15);
		$this->assign('list',$list);
		return $this->fetch();
	}
	
	
	
	public function cate_goods($name='',$agent_name=''){
		$where=[];
		if($name){
			$where['a.name']=array('like','%'.$name.'%');
		}
		$this->assign('name',$name);
		if($agent_name){
			$where['b.agent_name']=array('like','%'.$agent_name.'%');
		}
		$this->assign('agent_name',$agent_name);
		
		$list=db("good_cate")->alias("a")->join("agent b","a.agent_id=b.id")->where($where)->field("a.*,b.agent_name,b.mobile")->paginate(15);
		$this->assign('list',$list);
		return $this->fetch();
	}
	
	public function goods($is_on_sale='',$goods_name='',$agent_name=''){
		$where=[];
		if($goods_name){
			$where['a.goods_name']=['like', '%'.$goods_name.'%'];
		}
		$this->assign('goods_name',$goods_name);
		if($agent_name){
			$where['a.agent_name']=['like', '%'.$agent_name.'%'];
		}
		$this->assign('agent_name',$agent_name);
		if($is_on_sale){
			$where['a.is_on_sale']=$is_on_sale;
		}
		$this->assign('is_on_sale',$is_on_sale);
		$list=db("goods")->alias("a")->join("agent b","a.agent_id=b.id")->where($where)->field("a.*,b.agent_name")->paginate(15);
		$this->assign('list',$list);
		return $this->fetch();
	}
	
	
	//退货订单
	public function order_return($order_sn='',$status=''){
		 $where = [];
		if($order_sn){
			$where['order_sn']=['like', '%'.$order_sn.'%'];
		}
		if($status){
			$where['a.status']=$status;
		}
		$this->assign('status',$status);
		$this->assign('order_sn',$order_sn);
		$list=db("order_return")->alias("a")->join("member b","a.user_id=b.user_id")->where($where)->field("a.*,b.mobile")->paginate(15);
		$this->assign('list',$list);
		return $this->fetch();
	}
	//订单评价列表
	public function comments($order_sn='',$status=''){
		 $where = [];
		if($order_sn){
			$where['order_sn']=['like', '%'.$order_sn.'%'];
		}
		$this->assign('order_sn',$order_sn);
		if($status){
			$where['a.status']=$status;
		}
		$this->assign('status',$status);
		$list=db("good_comment")->alias("a")->join("good_order b","b.id=a.order_id")->where($where)->field("a.*,b.order_sn")->order("add_time desc")->paginate(15);
		$this->assign('list',$list);
		return $this->fetch();
	}
	//订单列表
    
    public function orders( $page = 1,$status='',$order_sn='',$agent_name='' ){
	    $field='m.*,g.goods_name,g.thumb,a.agent_name ,d.mobile';
		$where=[];
		if($status){
			$where['m.status']=$status;
		}
		$this->assign('status',$status);
		
		if($order_sn){
			$where['m.order_sn']=['like', '%'.$order_sn.'%'];
		}
		$this->assign('order_sn',$order_sn);
		
		if($agent_name){
			$where['a.agent_name']=['like', '%'.$agent_name.'%'];
		}
		$this->assign('agent_name',$agent_name);
		
	    $list = db('good_order')->alias('m')
	    ->field($field)
	    ->join('cz_ORDER_ITEMs g','g.order_id=m.id','LEFT')
	    ->join('cz_AGENT a','a.id=m.agent_id','LEFT')
	    ->join('cz_MEMBER d','d.user_id=m.user_id','LEFT')
		->where($where)
		->order("m.add_time desc")
	    ->paginate(3);
		
		$list1=array();
		if($list){
			foreach($list as $k=>$v){
				$list1[$v['id']]=$v;
			}
			$order_ids = array_keys($list1);						
			if($order_ids){
				$order_items = db("order_items")->where("order_id","in",$order_ids)->select();
				foreach($order_items as $k1=>$v1){
					 $list1[$v1['order_id']]['items'][]=$v1;
				}
			}
		}
		$this->assign("list1",$list1);
		$this->assign('list',$list);
		return $this->fetch();
    }
    public function shop_add(){
    	if(request()->isPost()){
			$data = $this->request->post();
			unset($data['file']);
			$data['password']=md5_salt(input('password'));
			$data['add_time'] = now_time();
			$result = add_data('agent',$data);
			if ($result) {
				$this->success('添加成功');
			} else {
				$this->error('添加失败');
			}
		}
    	return $this->fetch();
    	
    }
    
    
    
    public function money_activity($is_on_sale='',$goods_name='',$agent_name=''){
		$where=[];
		if($goods_name){
			$where['a.goods_name']=['like', '%'.$goods_name.'%'];
		}
		$this->assign('goods_name',$goods_name);
		if($agent_name){
			$where['a.agent_name']=['like', '%'.$agent_name.'%'];
		}
		$this->assign('agent_name',$agent_name);
		if($is_on_sale){
			$where['a.is_on_sale']=$is_on_sale;
		}
		$this->assign('is_on_sale',$is_on_sale);
		$list=db("goods")->alias("a")->join("agent b","a.agent_id=b.id")->where($where)->field("a.*,b.agent_name")->paginate(15);
		$this->assign('list',$list);
		return $this->fetch();
	}
}
